//****************************************************
// Porman Project 
// Filename: CRenderTarget.cpp
// Date: 2011/01/12
// Author: KengYu Lin
// Modify:
//****************************************************
#include <assert.h>
#include "CRenderTarget.h"

namespace Porman
{

namespace RS
{

CRenderTarget::CRenderTarget( const std::string& sName, unsigned int uiHash ) :
CRenderBase(sName,uiHash),
m_mapRenderViewports()
{
}

CRenderTarget::~CRenderTarget( void )
{
}

void CRenderTarget::initial( int iWidth, int iHeight )
{
    m_hRTData.getData().m_iWidth = iWidth;
    m_hRTData.getData().m_iHeight = iHeight;
}

void CRenderTarget::getMetrics( int& iWidth, int& iHeight )
{
    iWidth = m_hRTData.getData().m_iWidth;
    iHeight = m_hRTData.getData().m_iHeight;
}

bool CRenderTarget::addViewport( CRenderCamera* pCamera, int iZOrder, float fOffsetX, float fOffsetY, float fWidth, float fHeight )
{
    char sBuffer[100];
    _itoa_s( iZOrder, sBuffer, 100, 10 );
    CRenderViewport* pViewport = m_mapRenderViewports.createObj( sBuffer );
    if( pViewport )
    {
        pViewport->initial( this, pCamera, iZOrder, fOffsetX, fOffsetY, fWidth, fHeight );
        return true;
    }

    assert(0);
    return false;
}

bool CRenderTarget::deleteRenderViewport( int iZOrder )
{
    CRenderViewport* pViewport = getViewport( iZOrder );
    if( pViewport )
    {
        return m_mapRenderViewports.deleteObj( &(pViewport) );
    }
    return false;
}

void CRenderTarget::deleteAllRenderViewports( void )
{
    m_mapRenderViewports.deleteAllObj();
}

CRenderViewport* CRenderTarget::getViewport( int iZOrder )
{
    char sBuffer[100];
    _itoa_s( iZOrder, sBuffer, 100, 10 );
    return m_mapRenderViewports.getObj( sBuffer );
}

int CRenderTarget::getNumViewports( void )
{
    return m_mapRenderViewports.getNumObj();
}

void CRenderTarget::onSwap( void ) 
{ 
    m_mapRenderViewports.onMerge();
    m_mapRenderViewports.onSwap();
    m_mapRenderViewports.onClear();

    //-----------------------------------
    // Swap target data:
    //-----------------------------------
    m_hRTData.swap();
}

void CRenderTarget::onRender( void )
{
    //--------------------------
    // onRender:
    //--------------------------
    CRenderViewport* pViewport = m_mapRenderViewports.getFirst();
    while( pViewport )
    {
        pViewport->onRender();
        pViewport = m_mapRenderViewports.getNext( pViewport );
    }
}

}//End RS

}//End namespace Porman